package com.doget.data.web;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.webkit.JavascriptInterface;

//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
import com.gsls.gt.GT;

import org.json.JSONObject;

@SuppressLint("JavascriptInterface")
public class CommonJsInterface {

    private Activity activity;
    private WebviewCallbackListerner _listerner;

    public CommonJsInterface(Activity activity, WebviewCallbackListerner listerner) {
        this.activity = activity;
        _listerner = listerner;
    }

    @JavascriptInterface
    public void weAppPay(String responseData) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null)
                    _listerner.weAppPayCallback(responseData);
            }
        });
    }

    @JavascriptInterface
    public void askShare(String responseText) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
//
//                {"title":"",
//                        "description":"",
//                        "thumbImage":"",
//                        "webUrl":"",
//                        "shareGroupUrl":""}

                if (_listerner != null) {
//                    JSONObject obj = GT.JSON.parseObject(responseText);
//                    _listerner.askShare(
//                            obj.getString("title"),
//                            obj.getString("thumbImage"),
//                            obj.getString("webUrl"),
//                            obj.getString("shareGroupUrl")
//                    );
                }
            }
        });
    }

    @JavascriptInterface
    public void doClose() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.doClose();
                }
            }
        });
    }

    @JavascriptInterface
    public void startScanner() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.startScanner();
                }
            }
        });
    }

    @JavascriptInterface
    public void setTitle(String title) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.setTitle(title);
                }
            }
        });
    }

    @JavascriptInterface
    public void goToBillPay() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.goToBillPay();
                }
            }
        });
    }

    @JavascriptInterface
    public void goToAutoPay() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.goToAutoPay();
                }
            }
        });
    }

    @JavascriptInterface
    public void goToWXMimi(String param) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.goToWXMimi(param);
                }
            }
        });
    }
    @JavascriptInterface
    public void publicGoToWXMimi(String param) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.publicGoToWXMimi(param);
                }
            }
        });
    }

    @JavascriptInterface
    public void goToPage(String param) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.goToPage(param);
                }
            }
        });
    }

    @JavascriptInterface
    public void callUmengEvent(String eventId) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.callUmengEvent(eventId);
                }
            }
        });
    }


    @JavascriptInterface
    public void fromOnlineShoppingMallLogin(String param) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.fromOnlineShoppingMallLogin(param);
                }
            }
        });
    }

    @JavascriptInterface
    public void onlineShoppingMallEvent(String key, String json) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (_listerner != null) {
                    _listerner.onlineShoppingMallEvent(key, json);
                }
            }
        });
    }

    public interface WebviewCallbackListerner {
        void weAppPayCallback(String str);

        void askShare(String title, String thumbImage, String webUrl, String shareGroupUrl);

        void doClose();

        void startScanner();

        void setTitle(String title);

        void goToBillPay();

        void goToAutoPay();

        void goToWXMimi(String param);
        void publicGoToWXMimi(String param);

        void goToPage(String param);

        void callUmengEvent(String eventId);

        void fromOnlineShoppingMallLogin(String param);

        void onlineShoppingMallEvent(String key, String json);
    }

}
